#!/bin/bash

#PBS -lnodes=1:ppn=8
#PBS -lwalltime=04:00:00

# run_dynamic.sh
# roborobo
#
# Created by B.P.M. Weel on 3/20/12.
# Copyright 2012 VU. All rights reserved.

### Set the simulation-specific parameters
DIR=$(cd "$(dirname "$0")"; pwd -P)
BASEDIR="$(dirname $DIR)/"
BASEDIR="/home/kees/cworkspace/keesvangelder-changedetection-mercurial/"
TEMPLATEDIR=scripts/templates/
CONFNAME=changedetection-baselineevolution

FULLCOMMAND="$0 $@"
. ${BASEDIR}scripts/shflags

#define the flags
DEFINE_integer 'repeats' '50' 'The number of times the simulation should be run';
#DEFINE_string 'EnvironmentTemperature' '20' 'Initial temperature for the simulation';

#RANDOM=42

ROBOTS=1
ITERATIONS=1000000

# Parse the flags
FLAGS "$@" || exit 1
eval set -- "${FlAGS_ARGV}"

### Run the simulation
COUNTER=${FLAGS_repeats}
#ENVIRONMNENTTEMPERATURE=${FLAGS_EnvironmentTemperature}

echo "starting simulations, repeating $COUNTER times"
cd $BASEDIR

run_roborobo() {
    echo "Running experiment #$EXPERIMENT with parameters: --robots $ROBOTS --iterations $ITERATIONS --seed $SEED --basedir $BASEDIR --templatedir $TEMPLATEDIR --confname $CONFNAME"
    ${BASEDIR}scripts/run_changedetection_baseline.sh --robots $ROBOTS --iterations $ITERATIONS --seed $SEED --basedir $BASEDIR --templatedir $TEMPLATEDIR --confname $CONFNAME
}

cpu=8
MIN=1

COUNTER=${FLAGS_repeats}

until [ "$COUNTER" -lt "$MIN" ];  do
	count=$(jobs | wc -l)
    if [ $(($count-$cpu)) -lt 0 ]; then
    	SEED=$RANDOM
        let COUNTER-=1
        let EXPERIMENT=${FLAGS_repeats}-$COUNTER
        run_roborobo &
    fi
done

wait
